import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * GameBoard
 *
 * @author Jiachi Zhu
 * @date 2024/04/21
 */
public class GameBoard implements Serializable {

    public static final int BOARD_SIZE = 15;

    public static char[][] board = new char[BOARD_SIZE][BOARD_SIZE];

    public static final int BOARD_CENTER = 7;

    public static SpecialGrid.TileType[][] tileTypes = new SpecialGrid.TileType[BOARD_SIZE][BOARD_SIZE];

    public static char[][] enteredLetterBoard = new char[BOARD_SIZE][BOARD_SIZE];

    public static boolean[][] isEnterdBoard = new boolean[BOARD_SIZE][BOARD_SIZE];

    public static boolean firstWord = true;

    public static int winningScore = 100;


    /**
     * Initialize type board.
     */
    public static void initializeTypeBoard(){
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                tileTypes[i][j] = SpecialGrid.TileType.NORMAL;
            }

        }
    }

    /**
     * Initialize board.
     */
    public static void initializeBoard() {
        //重置棋盘的内容
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                tileTypes[i][j] = SpecialGrid.TileType.NORMAL;

            }

        }

        // 设置双字母方格 (DOUBLE_LETTER)
        int[][] doubleLetterCoords = {
                {0, 3}, {0, 11}, {2, 6}, {2, 8}, {3, 0}, {3, 7}, {3, 14},
                {6, 2}, {6, 6}, {6, 8}, {6, 12}, {7, 3}, {7, 11},
                {8, 2}, {8, 6}, {8, 8}, {8, 12}, {11, 0}, {11, 7},
                {11, 14}, {12, 6}, {12, 8}, {14, 3}, {14, 11}
        };
        for (int[] coord : doubleLetterCoords) {
            setSpecialTile(coord[0], coord[1], SpecialGrid.TileType.DOUBLE_LETTER);


        }

        // 设置三字母方格 (TRIPLE_LETTER)
        int[][] tripleLetterCoords = {
                {1, 5}, {1, 9}, {5, 1}, {5, 5}, {5, 9}, {5, 13},
                {9, 1}, {9, 5}, {9, 9}, {9, 13}, {13, 5}, {13, 9}
        };
        for (int[] coord : tripleLetterCoords) {
            setSpecialTile(coord[0], coord[1], SpecialGrid.TileType.TRIPLE_LETTER);
        }

        // 设置双字方格 (DOUBLE_WORD)
        int[][] doubleWordCoords = {
                {1, 1}, {1, 13}, {2, 2}, {2, 12}, {3, 3}, {3, 11},
                {4, 4}, {4, 10}, {7, 7}, {10, 4}, {10, 10}, {11, 3},
                {11, 11}, {12, 2}, {12, 12}, {13, 1}, {13, 13}
        };
        for (int[] coord : doubleWordCoords) {
            setSpecialTile(coord[0], coord[1], SpecialGrid.TileType.DOUBLE_WORD);
        }

        // 设置三字方格 (TRIPLE_WORD)
        int[][] tripleWordCoords = {
                {0, 0}, {0, 7}, {0, 14}, {7, 0}, {7, 14},
                {14, 0}, {14, 7}, {14, 14}
        };
        for (int[] coord : tripleWordCoords) {
            setSpecialTile(coord[0], coord[1], SpecialGrid.TileType.TRIPLE_WORD);
        }
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = '.';

                if (tileTypes[i][j] == SpecialGrid.TileType.DOUBLE_WORD) {
                    board[i][j] = '*';
                }
                if (tileTypes[i][j] == SpecialGrid.TileType.DOUBLE_LETTER) {
                    board[i][j] = '#';
                }
                if (tileTypes[i][j] == SpecialGrid.TileType.TRIPLE_LETTER) {
                    board[i][j] = '+';
                }
                if (tileTypes[i][j] == SpecialGrid.TileType.TRIPLE_WORD) {
                    board[i][j] = '-';
                }

            }
        }

        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                isEnterdBoard[i][j] = true;
            }
        }
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                enteredLetterBoard[i][j] = ' ';
            }
        }
    }

    /**
     * Print Letter board
     * @return the letter board
     */
    public static char[][] printBoard() {
        System.out.print("  "); // Space for row numbers
        for (int i = 1; i <= BOARD_SIZE; i++) {
            System.out.print(String.format("%2d ", i)); // Print column numbers
        }
        System.out.println();

        for (int i = 0; i < BOARD_SIZE; i++) {
            System.out.print(String.format("%2d ", i + 1)); // Print row number
            for (int j = 0; j < BOARD_SIZE; j++) {
                System.out.print(board[i][j] + "  ");

            }
            if(i == 4){
                System.out.print("\t\tLetter(s)\t\t\t\tWorth (in Points)");}
            if (i == 5){System.out.print("\t\tA,E,I,O,U,L,N,S,T,R\t\t1 point");}
            if (i == 6){ System.out.print("\t\tD, G\t\t\t\t\t2 points");}
            if (i == 7){ System.out.print("\t\tB, C, M, P\t\t\t\t3 points" );}
            if (i == 8){ System.out.print("\t\tF, H, V, W, Y \t\t\t4 points" );}
            if (i == 9){ System.out.print("\t\tK\t\t\t\t\t\t5 points" );}
            if (i == 10){ System.out.print("\t\tJ, X \t\t\t\t\t8 points");}
            if (i == 11){ System.out.print("\t\tQ, Z \t\t\t\t\t10 points" );}

            if (i == 12){
                System.out.print("\t\t* --- Double word ");
                System.out.print("\t\t\t# --- Double letter ");}
            if (i == 13) {System.out.print("\t\t- --- Triple word ");
                System.out.print("\t\t\t+ --- Triple letter ");
            }
            System.out.println();
        }
        return board;
    }

    /**
     * set the board of special tile
     *
     */
    private static void setSpecialTile(int row, int col, SpecialGrid.TileType type) {
        tileTypes[row][col] = type;
    }


}


